package com.sensei.search.nodes;

import static org.jboss.netty.channel.Channels.*;

import org.apache.lucene.queryParser.QueryParser;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
import org.jboss.netty.handler.codec.protobuf.ProtobufDecoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufEncoder;

import proj.zoie.api.IndexReaderFactory;

import com.browseengine.bobo.api.BoboIndexReader;
import com.browseengine.bobo.protobuf.BrowseRequestBPO;

public class SenseiServerPipelineFactory implements ChannelPipelineFactory {
	private final QueryParser _qparser;
	private final IndexReaderFactory<BoboIndexReader> _idxReaderFactory;
	
	public SenseiServerPipelineFactory(QueryParser qparser,IndexReaderFactory<BoboIndexReader> idxReaderFactory){
		_qparser = qparser;
		_idxReaderFactory = idxReaderFactory;
	}
	
	public ChannelPipeline getPipeline() throws Exception {
		 ChannelPipeline p = pipeline();
		 p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
		 p.addLast("protobufDecoder", new ProtobufDecoder(BrowseRequestBPO.Request.getDefaultInstance()));
		 p.addLast("frameEncoder", new LengthFieldPrepender(4));
		 p.addLast("protobufEncoder", new ProtobufEncoder());
		 p.addLast("handler", new SenseiServerHandler(_qparser,_idxReaderFactory));
		 return p;
	}
}
